<template>
  <view class="cate">
    <top-search></top-search>
    <view class="main">
      <!-- 左侧菜单 -->
      <view class="catelist">
        <view
          :class="['item', index === current ? 'active' : '']"
          v-for="(item, index) in menus"
          :key="item"
          @click="handleclick(index)"
          >{{ item }}</view
        >
      </view>
      <!-- 右侧商品 -->
      <view class="goods">
        <view
          class="goods-group"
          v-for="item1 in goodsList"
          :key="item1.cat_id"
        >
          <view class="goods-title">{{ item1.cat_name }}</view>
          <view class="goods-content">
            <view
              class="goods-item"
              v-for="item2 in item1.children"
              :key="item2.cat_id"
            >
              <image :src="item2.cat_icon"></image>
              <view class="goods-name">{{ item2.cat_name }}</view>
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      catelist: [],
      // 左侧要渲染的标题数组
      menus: [],
      // 右侧要显示的内容数据
      goodsList: [],
      // 选中标题的索引
      current: 0,
    };
  },
  async onLoad() {
    const category = uni.getStorageSync("category");
    if (!category) {
      console.log("没缓存  发送请求");
      this.getCategory();
    } else {
      console.log("有缓存 判断时间是否过期");
      if (Date.now() - category.time > 1000 * 10) {
        console.log("缓存过期 重新发送请求");
        this.getCategory();
      } else {
        console.log("缓存没过期");
        this.setCategory(category.data);
      }
    }
  },
  methods: {
    // 发送请求获取数据
    async getCategory() {
      const { message } = await uni.$u.http.get("/categories");
      this.setCategory(message);
    },

    // 设置页面要用到的数据
    async setCategory(message) {
      this.catelist = message;

      // 构造左侧标题数组
      this.menus = this.catelist.map((v) => v.cat_name);

      // 构造右侧要显示的商品数据
      this.goodsList = this.catelist[this.current].children;

      // 把数据存入到本地存储
      uni.setStorageSync("category", {
        data: this.catelist,
        time: Date.now(),
      });
    },

    // 左侧标题的点击
    handleclick(index) {
      this.current = index;
      this.goodsList = this.catelist[index].children;
    },
  },
  //     async getCategory(){
  //  const { message } = await uni.$u.http.get("/categories");
  //     this.setCategory(message);
  //     }

  // // 设置页面要用到的数据
  // async setCategory(message){
  //  this.catelist = message;
  //   // 构造左侧标题数组
  //     this.menus = this.catelist.map((v) => v.cat_name);
  //     // 构造右侧要显示的商品数据
  //     this.goodsList = this.catelist[this.current].children;

  //  // 把数据存到本地
  //     uni.setStorageSync('category',{
  //       data:this.catelist,
  //       time:Date.now()
  //        }),
  //     // async getcatelist() {
  //     //   const { message: catelist } = await uni.$u.http.get("/categories");
  //     //   this.catelist = catelist;
  //     //   console.log(catelist);
  //     // },

  //  },
  //   handleclick(index) {
  //       this.current = index;
  //       this.goodsList = this.catelist[index].children;
  //     },
  //   }
};
</script>

<style lang="scss">
.cate {
  height: 100vh;
  display: flex;
  flex-direction: column;
  .main {
    flex: 1;
    display: flex;
    overflow: hidden;
    .catelist {
      width: 182rpx;
      overflow: auto;
      .item {
        height: 80rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 28rpx;
        color: #666;
      }
      .active {
        color: #eb4450;
        border-left: 1px solid #eb4450;
      }
    }
    .goods {
      flex: 1;
      overflow: auto;
    }
  }
}
.goods {
  .goods-group {
    .goods-title {
      height: 80rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #666;
      font-size: 28rpx;
      &::before {
        content: "/";
        margin-right: 30rpx;
        color: #ccc;
      }
      &::after {
        color: #ccc;
        content: "/";
        margin-left: 30rpx;
      }
    }

    .goods-content {
      display: flex;
      flex-wrap: wrap;
      .goods-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 33.33%;
        image {
          width: 120rpx;
          height: 120rpx;
          background-color: #ccc;
        }
        .goods-name {
          color: #666;
          font-size: 28rpx;
        }
      }
    }
  }
}
</style>